
#### Google Trends Figure #### 
trend_df <- read.csv('data/trends_data.csv', header = T)
trend_df$trial_date <- as.numeric(trend_df$time=='2012-03', 1,0)
trend_df$year <- substring(trend_df$time, 1,4)
trend_df$year <- as.numeric(trend_df$year)
trend_df$month <- substring(trend_df$time, 6,7)
trend_df$day <- paste('01', trend_df$time, sep = '-')
trend_df$run <- 1:192
trend_df$date <- paste(trend_df$year, trend_df$month, '01', sep = '-')

trend_df$daten <- as.Date(trend_df$date)

trend_df$daten2 <- as.factor(trend_df$daten)

p1 <- ggplot(trend_df, aes(daten, search)) + 
  geom_line() +
  scale_x_date() + 
  geom_vline(aes(xintercept=as.numeric(daten[99])),
             linetype=2, colour="red") + 
  labs(title = 'Jan 2004 - Dec 2019', y = 'Search Popularity', x = 'Time') + 
  annotate("label", x = as.Date("2012-03-01"), y = 100, label = "Month of Verdict", 
           hjust=0, vjust=1, family="Times") +
  theme_tufte()


trend_sub_df <- read.csv('data/trends_lapop_subset.csv', header = T)
trend_sub_df$date <- as.character(trend_sub_df$day)
trend_sub_df$month <- substring(trend_sub_df$date, 1,2)
trend_sub_df$month <- gsub('/', '', trend_sub_df$month)
trend_sub_df$month <- str_pad(trend_sub_df$month, 2, pad = 0)


trend_sub_df$daymo <- substring(trend_sub_df$day, 3,4)
trend_sub_df$daymo <- gsub('/', '', trend_sub_df$daymo)
trend_sub_df$daymo <- str_pad(trend_sub_df$daymo, 2, pad = 0)

trend_sub_df$year <- substring(trend_sub_df$day, 6,7)

trend_sub_df$year <- str_pad(trend_sub_df$year, 2, pad = 1)
trend_sub_df$year <- paste0("20", trend_sub_df$year)


trend_sub_df$daten <- paste(trend_sub_df$year, trend_sub_df$month, trend_sub_df$daymo, sep ='-')
trend_sub_df$daten <- as.Date(trend_sub_df$daten)

p2 <- ggplot(trend_sub_df, aes(daten, search)) + 
  geom_line() +
  scale_x_date() + 
  geom_vline(aes(xintercept=as.numeric(daten[26])),
             linetype=2, colour="red") + 
  annotate("label", x = as.Date("2012-03-26"), y = 100, label = "Day of Verdict", 
           hjust=0, vjust=1, family="Times") +
  labs(title = '(March-April 2012)', 
       y = 'Search Popularity', x = 'Time') + 
  theme_tufte()

theme_set(theme_tufte())
plot = plot_grid(p1, p2, labels = "AUTO")
ggsave('fig-out/time_series_plots.pdf')
ggsave('fig-out/time_series_plots.png',dpi=600)
